package com.urbansquall.chaos{
	
import flash.utils.*;

public class RandomizedArray
{
	private var m_random : PseudoRandom;
	private var m_array : Array;
	
	public function RandomizedArray( a_random : PseudoRandom = null )
	{
		if( a_random == null )
		{
			a_random = new PseudoRandom( getTimer() );
		}
		
		m_random = a_random;
		m_array = new Array();
	}
		
	public function insert( a_object : Object ) : void
	{
		var randomizedIndex : int = Math.floor( m_random.randomRange( 0, this.length ) );
		
		m_array.splice( randomizedIndex, 0, a_object );
	}
	
	public function getObjectAt( a_index : int ) : Object
	{
		return m_array[ a_index ];
	}
	
	public function randomize() : void
	{
		var copy : Array = m_array.slice();
		
		m_array.splice( 0 );
		
		for( var i : int = 0; i < copy.length; i++ )
		{
			insert( copy[ i ] );
		}
	}
	
	public function get length() : int { return m_array.length; }
}

}